//--------------------------------------------------------------------------------------------
//
//      Component name  : fpalign
//      Author          : 
//      Company         : 
//
//      Description     : 
//
//
//--------------------------------------------------------------------------------------------


module FPalign(A_in, B_in, cin, diff, A_out, B_out);
   input [28:0]  A_in;
   input [28:0]  B_in;
   input         cin;
   input [8:0]   diff;
   output [28:0] A_out;
   output [28:0] B_out;
   reg [28:0]    B_out;
   
   
   reg [28:0]    B_shift;
   reg [8:0]     diff_int;
   reg [5:0]     shift_B;
  
   wire [8:0]     abs_diff_int;  
   assign   abs_diff_int = ~diff_int + 1 ;
   
   always @(diff_int or B_shift)
      if (diff_int[8] == 1'b1)
      begin
         //if ((((~diff_int)) + 1) > 28)
         if (  (abs_diff_int) > 28)
            B_out <= {29{1'b0}};
         else
            B_out <= B_shift;
      end
      else
         if (diff_int > 28)
            B_out <= {29{1'b0}};
         else
            B_out <= B_shift;
   
   
   always @(diff_int)
      if (diff_int[8] == 1'b1)
         shift_B <= ((~diff_int[5:0])) + 1;
      else
         shift_B <= diff_int[5:0];
   
   
   always @(cin or diff)
      if ((cin == 1'b1) & (diff[8] == 1'b1))
         diff_int <= diff + 2;
      else
         diff_int <= diff;
   
   assign A_out = A_in;
   
   
   always @(B_in or shift_B)
   begin: I1combo
      reg [5:0]     stemp;
      reg [28:0]    dtemp;
      reg [28:0]    temp;
      integer       i;
      temp = {29{1'bX}};
      stemp = shift_B;
      temp = B_in;
      for (i = 5; i >= 0; i = i - 1)
      begin
         if (i < 5)
         begin
            if (stemp[i] == 1'b1 )
            begin
               dtemp = {29{1'b0}};
               //dtemp[28 - 2 ** i:0] = temp[28:2 ** i];
               if(i==4)        dtemp[28 - 16:0] = temp[28:16];
               else if (i==3)  dtemp[28 - 8:0] = temp[28:8];
               else if (i==2)  dtemp[28 - 4:0] = temp[28:4];
               else if (i==1)  dtemp[28 - 2:0] = temp[28:2];
               else            dtemp[28 - 1:0] = temp[28:1];
            end
            else if (stemp[i] == 1'b0 )
               dtemp = temp;
            else
               dtemp = {29{1'bX}};
         end
         else begin 
            if (stemp[i] == 1'b1 )
               dtemp = {29{1'b0}};
            else if (stemp[i] == 1'b0 )
               dtemp = temp;
            else
               dtemp = {29{1'bX}};
         end
         temp = dtemp;
      end
      B_shift = dtemp;
   end
   
endmodule
